<?php
if (!defined('PRODUCTION')) die ("This resource is unavailable and/or is a server end-point");
/**
 * Resolves the URI by first splitting it by the ParseURI Class
 * Then Decides what is the page or post perma address that needs to be loaded.
 *
 * @author Agrim Gupta
 * @package OrangeRinds
 * @subpackage OrangeRinds Classes
 * @category URI Resolution
 * @since 0.0.1
 */
class URIResolve extends URISplit{
    /** @var string Holds the permalink of the post/page found, if no page exists, its value will be '404' */
    var $foundWhat;

    /** @var int Copy of the REQ_URI_PATH_E_LEN variable for shorter access */
    var $pLen;

    /** @var string Holds the name of the field to be searched for first, in this case - 'pages' */
    var $field1;

    /** @var string Holds the name of the field to be searched for second, in this case - 'posts' */
    var $field2;

    /** @var string Holds the type of the post found - page or post */
    var $postType;

    function __construct($uri) {
        parent::__construct($uri);
        $this->field1 = 'page_perma';
        $this->field2 = 'post_perma';
        $this->postType = 'page_perma';
        $this->resolve_uri();
    }

    /** resolves the uri and finds out what page needs to be loaded. */
    function resolve_uri() {
        $this->pLen = $this->REQ_URI_PATH_E_LEN;

        $this->check_presence($this->field1);
        if ($this->foundWhat == '')
        {
            $this->check_presence($this->field2);
            if( $this->foundWhat == '') {
                $this->foundWhat = 'home';
            }
            else {
                $this->postType = 'post_perma';
            }
        }
        else
        {
            $oldFoundWhat = $this->foundWhat;

            $this->check_presence($this->field2);
            if( $this->foundWhat == ''){
                $this->foundWhat = $oldFoundWhat;
            }
            else {
                $this->postType = 'post_perma';
            }
        }
    }

    /**
     * checks presence of all elements in REQ_URI_PATH_E array,
     * the first element to be reported to be found is accepted, and stored in this-foundWhat
     * Otherwise makes this-foundWhat equal to ''
     *
     * @param string $whereToFind This is the name of the field in which searching has to be done.
     */
    function check_presence($whereToFind) {
        $found = false;

        if      ($whereToFind == $this->field1) {$tableName = TABLE_PAGES;  }
        else if ($whereToFind == $this->field2) {$tableName = TABLE_POSTS;  }
        else                                  {$whereToFind = $this->field2;}

        for ( $i = 0; ($i < $this->pLen) && ($found == false) ; $i++ )
        {
            $whatToFind = $this->REQ_URI_PATH_E[$i];
            $newPageQuery = "SELECT `".$whereToFind
                           ."` FROM `".DB_NAME."`.`".$tableName
                           ."` WHERE `".$whereToFind."` = '".$whatToFind
                           ."' LIMIT 0 , 30";

            $newQuery = new QueryClass($newPageQuery);
            $arr = $newQuery->get_new_query_array();

            if($arr) {
                $found = true;
                $this->foundWhat  = $whatToFind;
            }
            else {
                $this->foundWhat = '';
            }
        }
    }
}
?>